package com.xoa.service.attendance.impl;

import com.sun.org.apache.bcel.internal.generic.SWITCH;
import com.sun.org.apache.xerces.internal.util.LocatorWrapper;
import com.xoa.dao.attendance.LeaveStatisticsMapper;
import com.xoa.dao.book.BookInfoMapper;
import com.xoa.dao.book.BookManageMapper;
import com.xoa.dao.book.BookTypeMapper;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.users.UserPrivMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.attendance.LeaveStatistics;
import com.xoa.model.book.BookInfoWithBLOBs;
import com.xoa.model.users.UserPriv;
import com.xoa.model.users.Users;
import com.xoa.service.attendance.LeaveStatisticsService;
import com.xoa.util.DateFormat;
import com.xoa.util.ExcelUtil;
import com.xoa.util.FileUploadUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;

import static com.xoa.util.FileUploadUtil.allowUpload;


/**
 * 创建作者:   王禹萌
 * 创建日期:   2018年6月27日 下午15：00
 * 类介绍  :请假申请service
 * 构造参数:
 */
@Service
public class LeaveStatisticsServiceImpl implements LeaveStatisticsService {
    @Resource
    LeaveStatisticsMapper leaveStatisticsMapper;

    @Override
    public ToJson leaveStatisticsQuery(String DATA_162, String DATA_163, String role,Integer page,
                                       Integer pageSize, boolean useFlag) {
        ToJson<LeaveStatistics> toJson = new ToJson<LeaveStatistics>();
        Map<String,Object>maps = new HashMap<String,Object>();
        PageParams pageParams = new PageParams();
        try{
            if(useFlag){
                pageParams.setPage(page);
                pageParams.setPageSize(pageSize);
                pageParams.setUseFlag(useFlag);
            }
            maps.put("page", pageParams);
            maps.put("DATA_162",DATA_162);
            maps.put("DATA_163",DATA_163);
            String str = "Query";
            Map map = new HashMap();
            map.put("DATA_162",DATA_162);
            map.put("DATA_163",DATA_163);
            int count = 0;
            List<LeaveStatistics>leaveList= null;
            if(role.equals("0")){
                leaveList= leaveStatisticsQuery(map,role);
            }else{
                leaveList= leaveStatisticsQuery(maps,role);
            }
            List<LeaveStatistics>leaveL= null;
            switch(role){
                case "0"://全部类型
                    count = leaveList.size();
                    break;
                case "1"://班主任
                    leaveL = leaveStatisticsMapper.leaveStatisticsHeadTeacherQuery(map);
                    count = leaveL.size();
                    break;
                case "2"://非班主任
                    leaveL = leaveStatisticsMapper.leaveStatisticsNoHeadTeacherQuery(map);
                    count = leaveL.size();
                    break;
                case "3"://总务后勤
                    leaveL = leaveStatisticsMapper.leaveStatisticsGeneralLogisticsQuery(map);
                    count = leaveL.size();
                    break;
                case "4"://行政
                    leaveL = leaveStatisticsMapper.leaveStatisticsAdministrativeQuery(map);
                    count = leaveL.size();
                    break;

            }
            toJson.setObj(leaveList);
            toJson.setTotleNum(count);
            toJson.setFlag(0);
            toJson.setMsg("ok");

        } catch (Exception e) {
            toJson.setFlag(1);
            toJson.setMsg("err");
            e.printStackTrace();
        }

        return toJson;
    }

    @Override
    public LeaveStatistics leaveStatisticsExport(HttpServletRequest request, HttpServletResponse response, String DATA_162, String DATA_163, String role) {
        Map<String,Object>maps = new HashMap<String,Object>();
        maps.put("DATA_162",DATA_162);
        maps.put("DATA_163",DATA_163);
        String str ="export";
        List<LeaveStatistics>leaveList= leaveStatisticsQuery(maps,role);
        HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("请假信息", 9);
        String[] secondTitles = {"请假流水号", "姓名", "教研组", "年级组", "请假类型", "请假开始时间", "请假结束时间", "共计天数"};
        HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
        String[] beanProperty=null;
        List<LeaveStatistics>leaveL=new ArrayList<LeaveStatistics>();
        switch (role){
            case "0"://全部
               for(LeaveStatistics LeaveStatistics:leaveList){
                   if(LeaveStatistics.getDATA_173()!=null&&LeaveStatistics.getDATA_173()!=""){
                       LeaveStatistics.setDATA_174(LeaveStatistics.getDATA_173());
                   }
                   if(LeaveStatistics.getDATA_169()!=null&&LeaveStatistics.getDATA_169()!=""){
                       LeaveStatistics.setDATA_174(LeaveStatistics.getDATA_169());
                   }
               }
                beanProperty =new String[]{"RUN_ID", "DATA_154", "DATA_164", "DATA_174", "DATA_95", "DATA_162", "DATA_163", "DATA_17"};
                break;
            case "1":
                beanProperty =new String[]{"RUN_ID", "DATA_154", "DATA_164", "DATA_174", "DATA_95", "DATA_162", "DATA_163", "DATA_17"};
                break;
            case "2":
                beanProperty =new String[]{"RUN_ID", "DATA_154", "DATA_164", "DATA_173", "DATA_95", "DATA_162", "DATA_163","DATA_17"};
                break;
            case "3":
                beanProperty =new String[]{"RUN_ID", "DATA_154", "DATA_164", "DATA_95", "DATA_162", "DATA_163","DATA_17"};
                break;
            case "4":
                beanProperty =new String[]{"RUN_ID", "DATA_154", "DATA_164", "DATA_169","DATA_95", "DATA_162", "DATA_163","DATA_17"};
                break;
        }
        try{
            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, leaveList, beanProperty);
            ServletOutputStream out = response.getOutputStream();

            String filename = "请假人员信息表.xls";
            filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition", "attachment;filename=" + filename);
            workbook.write(out);
            return null;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public List<LeaveStatistics> leaveStatisticsQuery(Map maps,String role){
        List<LeaveStatistics>leaveList=new ArrayList<LeaveStatistics>();
        List<LeaveStatistics>leaveL=new ArrayList<LeaveStatistics>();
        switch (role){
            case "0"://全部
                List<LeaveStatistics> leaveList1 = null;
                List<LeaveStatistics> leaveList2 = null;
                List<LeaveStatistics> leaveList3 = null;
                List<LeaveStatistics> leaveList4 = null;
                leaveList1 = leaveStatisticsMapper.leaveStatisticsHeadTeacherQuery(maps);
                leaveList2 = leaveStatisticsMapper.leaveStatisticsNoHeadTeacherQuery(maps);
                leaveList3 = leaveStatisticsMapper.leaveStatisticsGeneralLogisticsQuery(maps);
                leaveList4 = leaveStatisticsMapper.leaveStatisticsAdministrativeQuery(maps);
                for(int i =0;i<leaveList1.size();i++){
                    leaveL.add(leaveList1.get(i));
                }
                for(int i =0;i<leaveList2.size();i++){
                    leaveL.add(leaveList2.get(i));
                }
                for(int i =0;i<leaveList3.size();i++){
                    leaveL.add(leaveList3.get(i));
                }
                for(int i =0;i<leaveList4.size();i++){
                    leaveL.add(leaveList4.get(i));
                }
                Map<String,List<LeaveStatistics>>tempMap = new HashMap<String,List<LeaveStatistics>>();
                int i =0;
                int j =0;
                int c =0;
                for(LeaveStatistics LeaveStatistics:leaveL){
                    if(i==0){
                        List<LeaveStatistics> tempList = new ArrayList<LeaveStatistics>();
                        tempList.add(LeaveStatistics);
                        tempMap.put(LeaveStatistics.getDATA_154(),tempList);
                        i = i+1;
                        j = 1;
                        c = c+1;
                    }else{
                        c = 0;
                    }
                    if(tempMap.keySet().toString().indexOf(LeaveStatistics.getDATA_154())!=-1){
                        if(c ==1){

                        }else{
                            try{
                                List<LeaveStatistics> tempList1 = (List)tempMap.get(LeaveStatistics.getDATA_154());
                                tempList1.add(LeaveStatistics);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }

                    }else{
                        if(tempMap.keySet().toString().indexOf(LeaveStatistics.getDATA_154())!=-1){
                            List<LeaveStatistics> tempList1 = (List)tempMap.get(LeaveStatistics.getDATA_154());
                            tempList1.add(LeaveStatistics);
                        }else{
                          /*  if(j == 1){
                                j = j+1;
                            }else{*/
                            List<LeaveStatistics> tempList2 = new ArrayList<LeaveStatistics>();
                            tempList2.add(LeaveStatistics);
                            tempMap.put(LeaveStatistics.getDATA_154(),tempList2);
                            //  }

                        }
                    }
                }
                Set keys = tempMap.keySet( );
                if(keys != null) {
                    Iterator iterator = keys.iterator( );
                    while(iterator.hasNext( )) {
                        Object key = iterator.next( );
                        List<LeaveStatistics> list = tempMap.get(key);
                        for(int k =0;k<list.size();k++){
                            leaveList.add(list.get(k));
                        }
                    }
                }
                break;
            case "1"://班主任
                leaveList = leaveStatisticsMapper.leaveStatisticsHeadTeacherQuery(maps);
                break;
            case "2"://非班主任
                leaveList = leaveStatisticsMapper.leaveStatisticsNoHeadTeacherQuery(maps);
                break;
            case "3"://总务后勤
                leaveList = leaveStatisticsMapper.leaveStatisticsGeneralLogisticsQuery(maps);
                break;
            case "4"://行政
                leaveList = leaveStatisticsMapper.leaveStatisticsAdministrativeQuery(maps);
                break;
        }
        return leaveList;
    }


}
